# -*- coding: utf-8 -*-
import xlrd,os
import shutil
def gaiming(a):
    key=str(a.keys()).replace('dict_keys([','').replace('])','').replace('\\\\','\\').replace('\'','').replace('\"','')
    value=str(a.values()).replace('dict_values([','').replace('])','').replace('\\\\','\\').replace('\'','').replace('\"','')
    try:
        if key!=value:
            os.rename(key,value)
            # print(key,value,'改名成功...')
        else:
            pass
            # print('改名时路径和名称一致,跳过...')
    except (FileNotFoundError,FileExistsError) as e:
        print('文件存在异常:',e)

def jianqie(a):
    key = str(a.keys()).replace('dict_keys([', '').replace('])', '').replace('\\\\', '\\').replace('\'', '').replace('\"', '')
    value = str(a.values()).replace('dict_values([', '').replace('])', '').replace('\\\\', '\\').replace('\'','').replace('\"', '')
    try:
        #提取文件的文件夹
        path=os.path.dirname(os.path.realpath(value))
        #如果文件夹不存在，就创建
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            if key != value:
                shutil.move(key,value)
                # print('剪切成功...')
            else:
                pass
                # print('剪切时路径和名称一致,跳过...')
    except (FileNotFoundError,FileExistsError) as e:
        print('文件存在异常:',e)
def copys(a):
    key = str(a.keys()).replace('dict_keys([', '').replace('])', '').replace('\\\\', '\\').replace('\'', '').replace('\"', '')
    value = str(a.values()).replace('dict_values([', '').replace('])', '').replace('\\\\', '\\').replace('\'','').replace('\"', '')
    try:
        #提取文件的文件夹
        path=os.path.dirname(os.path.realpath(value))
        #如果文件夹不存在，就创建
        if not os.path.exists(path):
            os.makedirs(path)
            shutil.copy(key, value)
        else:
            if key != value:
                shutil.copy(key,value)
                print('拷贝成功...')
            else:
                pass
                # print('拷贝时路径和名称一致,跳过...')
    except (FileNotFoundError,FileExistsError) as e:
        print('文件存在异常:',e)


def dels(a):
    key = str(a.keys()).replace('dict_keys([', '').replace('])', '').replace('\\\\', '\\').replace('\'', '').replace('\"', '')
    # value = str(a.values()).replace('dict_values([', '').replace('])', '').replace('\\\\', '\\').replace('\'','').replace('\"', '')
    try:
        print('正在删除', key)
        os.remove(key)
        # print('  删除成功...')
    except (FileNotFoundError,FileExistsError) as e:
        print('文件存在异常:',e)


def read_excel():
    file = input('请输入excel文件地址,A列为原始文件名,B列为要改的名 不要表头:').replace('\"', '')
    workbook = xlrd.open_workbook(r'%s' % file)
    sheet1_name = workbook.sheet_names()[0]
    sheet = workbook.sheet_by_name(sheet1_name)
    print('行数是:', sheet.nrows)
    list = []
    if vls == 'A' or vls == 'a':
        defs = gaiming
    elif vls == 'J' or vls == 'j':
        defs = jianqie
    elif vls == 'C' or vls == 'c':
        defs = copys
    elif vls == 'd' or vls == 'D':
        defs = dels
    else:
        print('没有选择正确的命令，请重新运行程序')
        input()
        exit()
    n=0
    for i in range(0,sheet.nrows):
        alist=sheet.cell(i, 0).value
        if defs!=dels:
            blist = sheet.cell(i, 1).value
            print("%d/%d"%(n,sheet.nrows),{alist:blist})
            po.apply_async(defs, ({alist:blist},))
            n+=1
        else:
            po.apply_async(defs, ({alist:''},))

if __name__ == '__main__':
    from multiprocessing import Pool
    po = Pool(30)
    vls = input('A=重命名 J=剪切 C=拷贝 D=删除:')
    read_excel()
    print('操作完成')
    print('---start-----')
    po.close()
    po.join()
    print('end')
